package com.easylinkin.linkappapi.crane.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.easylinkin.linkappapi.common.model.RequestModel;
import com.easylinkin.linkappapi.crane.dto.CraneDTO;
import com.easylinkin.linkappapi.crane.entity.CraneTree;
import com.easylinkin.linkappapi.mechanical.entity.Mechanical;
import com.easylinkin.linkappapi.mechanical.service.CraneSafeMonitorService;
import com.easylinkin.linkappapi.security.context.LinkappUserContextProducer;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import site.morn.rest.RestBuilders;
import site.morn.rest.RestMessage;

import java.util.List;
import java.util.Map;

/**
 * 吊车安全监测 前端控制器
 *
 * @author xy
 * @since 2025-06-16
 */
@Api(tags = "吊车安全监测")
@RestController
@RequestMapping("/crane/safe/monitor")
public class CraneSafeMonitorController {

    @Autowired
    private CraneSafeMonitorService craneSafeMonitorService;

    @Autowired
    private LinkappUserContextProducer linkappUserContextProducer;


    /**
     * 今日侵限统计
     *
     * 侵限预警机械数量：当前有施工防侵限或防倾倒侵限的机械的数量；
     * 机械北斗定位总数量
     *
     * @return {"beidouCount": 0, "totalCount": 0, "warningCount": 0}
     *
     */
    @ApiOperation(value="今日侵限统计", notes="今日侵限统计")
    @GetMapping("/todayInfringementLimit")
    public RestMessage queryToday() {
        Map<String, Long> todayInfringementLimit = craneSafeMonitorService.todayInfringementLimit();
        return RestBuilders.successBuilder().data(todayInfringementLimit).build();
    }

    /**
     * 今日告警分类统计
     *
     * @return
     */
    @ApiOperation(value="今日告警分类统计", notes="今日告警分类统计")
    @GetMapping("/todayAlarmClassification")
    public RestMessage queryTodayAlarmClassification() {
        Map<String, Object> todayAlarmClassification = craneSafeMonitorService.todayAlarmClassification();
        return RestBuilders.successBuilder().data(todayAlarmClassification).build();
    }


    /**
     * 获取机械列表
     *
     * @return
     */
    @ApiOperation(value="机械列表", notes="机械列表")
    @GetMapping("/mechanical")
    public RestMessage queryMechanical() {
        List<CraneDTO> mechanicalList = craneSafeMonitorService.queryMechanical();
        return RestBuilders.successBuilder().data(mechanicalList).build();
    }

    /**
     * 分页列表查询
     *
     * @param requestModel
     * @return
     */
    @ApiOperation(value="分页列表查询", notes="分页列表查询")
    @PostMapping(value = "/listPage")
    public RestMessage queryPageList(@RequestBody RequestModel<Mechanical> requestModel) {
        // Assert.notNull(requestModel,"参数不能为空");
        // Assert.notNull(requestModel.getCustomQueryParams(), "customQueryParams 不能为空");
        Assert.notNull(requestModel.getPage(), "page 不能为空");
        requestModel.getCustomQueryParams().setTenantId(linkappUserContextProducer.getNotNullCurrent().getTenantId());
        IPage<CraneDTO> pageList = craneSafeMonitorService.listPage(requestModel);
        return RestBuilders.successBuilder().data(pageList).build();
    }

    /**
     * 获取吊车树列表
     *
     * @return
     */
    @ApiOperation(value="吊车树列表", notes="吊车树列表")
    @GetMapping("/craneTree")
    public RestMessage queryCraneTree() {
        List<CraneTree> craneTreeList = craneSafeMonitorService.queryCraneTree();
        return RestBuilders.successBuilder().data(craneTreeList).build();
    }


}
